#include <iostream>


struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right)
        : val(x), left(left), right(right) {}
};

bool isSame(TreeNode* t1, TreeNode* t2) {
    if (t1 == nullptr && t2 == nullptr) return 1;
    if (t1 == nullptr || t2 == nullptr) return 0;
    if (t1->val == t2->val)
        return isSame(t1->left, t2->left) && isSame(t1->right, t2->right);
    else return 0;
}

bool isSubtree(TreeNode* root, TreeNode* subRoot) {
    if(isSame(root, subRoot)) return 1;
    if(root->left == nullptr && root->right == nullptr) return 0;
    if(root->left == nullptr) return isSubtree(root->right, subRoot);
    if(root->right == nullptr) return isSubtree(root->left, subRoot);
    return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
} 